package com.ruoyi.record.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.record.common.RecordConstant;
import com.ruoyi.record.domain.WordStudyStep;
import com.ruoyi.record.service.IWordStudyStepService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.WordStudyStepProgressMapper;
import com.ruoyi.record.domain.WordStudyStepProgress;
import com.ruoyi.record.service.IWordStudyStepProgressService;

/**
 * 单词学习进程Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@Slf4j
@Service
public class WordStudyStepProgressServiceImpl implements IWordStudyStepProgressService
{
    @Autowired
    private WordStudyStepProgressMapper wordStudyStepProgressMapper;

    private IWordStudyStepService wordStudyStepService;

    @Autowired
    private WordRecordCommonService recordCommonService;

    public WordStudyStepProgressServiceImpl(@Lazy IWordStudyStepService wordStudyStepService) {
        this.wordStudyStepService = wordStudyStepService;
    }

    /**
     * 查询单词学习进程
     *
     * @param id 单词学习进程主键
     * @return 单词学习进程
     */
    @Override
    public WordStudyStepProgress selectWordStudyStepProgressById(Long id)
    {
        return wordStudyStepProgressMapper.selectWordStudyStepProgressById(id);
    }

    /**
     * 查询单词学习进程列表
     *
     * @param wordStudyStepProgress 单词学习进程
     * @return 单词学习进程
     */
    @Override
    public List<WordStudyStepProgress> selectWordStudyStepProgressList(WordStudyStepProgress wordStudyStepProgress)
    {
        return wordStudyStepProgressMapper.selectWordStudyStepProgressList(wordStudyStepProgress);
    }

    /**
     * 筛选过期的任务数据
     * */
    @Override
    public List<WordStudyStepProgress> selectStepProgressList(WordStudyStepProgress wordStudyStepProgress)
    {
        return wordStudyStepProgressMapper.selectStepProgressList(wordStudyStepProgress);
    }

    /**
     * 新增单词学习进程
     *
     * @param wordStudyStepProgress 单词学习进程
     * @return 结果
     */
    @Override
    public int insertWordStudyStepProgress(WordStudyStepProgress wordStudyStepProgress)
    {
        wordStudyStepProgress.setStatus(RecordConstant.STATUS_NORMAL);
        wordStudyStepProgress.setTime(DateUtils.getNowDate());
        int id = wordStudyStepProgressMapper.insertWordStudyStepProgress(wordStudyStepProgress);

        //处理单词的学习测试任务
        if (wordStudyStepProgress.getType() == RecordConstant.TASK_TYPE_WORD_STUDY ||
                wordStudyStepProgress.getType() == RecordConstant.TASK_TYPE_WORD_TEST) {
            recordCommonService.getAndSet(wordStudyStepProgress);
        }
        return id;
    }

    /**
     * 修改单词学习进程
     *
     * @param wordStudyStepProgress 单词学习进程
     * @return 结果
     */
    @Override
    public int updateWordStudyStepProgress(WordStudyStepProgress wordStudyStepProgress)
    {
        //流程取消的处理
        Integer active = wordStudyStepProgress.getActive();
        if (active == RecordConstant.ACTIVE_COMPLETE){
            wordStudyStepProgress.setComplete(DateUtils.getNowDate());
        }
        if (active == RecordConstant.ACTIVE_RECEIVE){
            wordStudyStepProgress.setReceive(DateUtils.getNowDate());
        }
        if (active == RecordConstant.ACTIVE_CANCEL){
            JSONObject jsonObj = new JSONObject();
            wordStudyStepProgress.setCancel(DateUtils.getNowDate());
            jsonObj.put(RecordConstant.TASK_CANCEL_ID, wordStudyStepProgress.getId());
            recordCommonService.delTodoTaskData(
                    RecordConstant.TASK_DATA_TO_REDIS + wordStudyStepProgress.getStudentId(),jsonObj);
        }
        int updated = wordStudyStepProgressMapper.updateWordStudyStepProgress(wordStudyStepProgress);
        log.info("流程中-id:{}-状态:{}-数据更新",wordStudyStepProgress.getId(), active);
        //更新主表
        updateMainTable(wordStudyStepProgress.getId());
        return updated;
    }

    public void updateMainTable(Long id){
        WordStudyStepProgress progress = wordStudyStepProgressMapper.selectWordStudyStepProgressById(id);
        if (progress == null){
            log.info("主任务未查询到-不再处理-查询编号:{}",id);
            return;
        }
        if (progress.getActive().equals(RecordConstant.ACTIVE_COMPLETE)) {
            WordStudyStep studyStep = wordStudyStepService.selectWordStudyStepById(Long.valueOf(progress.getMain()));
            Integer stepSend = studyStep.getSend();
            log.info("判断所有用户是否已完成,总人数:{},当前人数:{},总步骤:{},当前步骤:{}",
                    studyStep.getComplete(), studyStep.getSend(),studyStep.getTotal(),progress.getStep());
            //判断是否是最后一步
            if (studyStep.getTotal().equals(progress.getStep()) || progress.getTotal() == 0){
                if (stepSend != null ) {
                    studyStep.setSend(stepSend + 1);
                }
                else {
                    studyStep.setSend(1);
                }
                //判断人数是否已全部完成
                if (studyStep.getSend().equals(studyStep.getComplete())){
                    studyStep.setActive(RecordConstant.ACTIVE_COMPLETE);
                }
            }
            wordStudyStepService.updateWordStudyStep(studyStep);
        }
    }

    /**
     * 批量删除单词学习进程
     *
     * @param ids 需要删除的单词学习进程主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyStepProgressByIds(Long[] ids)
    {
        return wordStudyStepProgressMapper.deleteWordStudyStepProgressByIds(ids);
    }

    /**
     * 删除单词学习进程信息
     *
     * @param id 单词学习进程主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyStepProgressById(Long id)
    {
        return wordStudyStepProgressMapper.deleteWordStudyStepProgressById(id);
    }
}
